package com.xiaolong.redis.aspectj;

import com.xiaolong.common.exception.BusinessException;
import com.xiaolong.redis.annotation.CacheEvict;
import com.xiaolong.redis.util.CacheUtil;
import com.xiaolong.redis.util.PointUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Aspect
public class CacheEvictAspect {

    private static final Logger logger = LoggerFactory.getLogger(LockAspect.class);

    ScheduledExecutorService executorService = Executors.newScheduledThreadPool(5);

    @Pointcut("@annotation(com.xiaolong.redis.annotation.CacheEvict)")
    public void lockPointCut() {
    }

    @After("lockPointCut()")
    public void after(ProceedingJoinPoint point) {
        CacheEvict cache = PointUtil.getInstance(point, CacheEvict.class);
        CacheUtil.del(cache.key());
        executorService.schedule(new Runnable() {
            @Override
            public void run() {
                CacheUtil.del(cache.key());
            }
        }, 1, TimeUnit.SECONDS);

    }

}
